import { fileURLToPath, URL } from "node:url";
import path from "path";
import fs from "fs";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";
import { basePath } from "./src/config";

const alias = [{ find: "@", replacement: fileURLToPath(new URL("./src", import.meta.url)) }];
const sunadminCorePath = "../../npm-core";
const coreSrcPath = path.resolve(fileURLToPath(new URL(".", import.meta.url)), `${sunadminCorePath}/src`);
const HAS_LOCAL_SUNADMIN_CORE = fs.existsSync(coreSrcPath);
if (HAS_LOCAL_SUNADMIN_CORE) {
	alias.push({
		find: "sunadmin-core/dist/style.css",
		replacement: path.resolve(__dirname, `${sunadminCorePath}/dist/style.css`),
	});
	alias.push({
		find: "sunadmin-core",
		replacement: coreSrcPath,
	});
}
// https://vite.dev/config/
export default defineConfig({
	plugins: [vue(), vueDevTools()],
	base: basePath, // 设置你的项目部署在子路径
	resolve: {
		alias: alias,
	},
	define: {
		__HAS_LOCAL_SUNADMIN_CORE__: JSON.stringify(HAS_LOCAL_SUNADMIN_CORE),
	},
});

function showWelcome() {
	const logo = `
  ______                     ___         _           _        
 / _____)                   (___)       | |         (_)       
( (____    _   _   ____    /_____\\    __| |  ____    _   ____  
 \\____ \\  | | | | |  _ \\  |  ___  |  / _  | |    \\  | | |  _ \\ 
 _____) ) | |_| | | | | | | |   | | ( (_| | | | | | | | | | | |
(______/  |____/  |_| |_| |_|   |_|  \\____| |_|_|_| |_| |_| |_|
`;
	console.log(logo);
	console.log("📟 您只需要专注于开发业务逻辑,无侵入性的便捷更新SunAdmin核心包");
	console.log("📟 文档地址:https://gitee.com/sunjinglong/create-sunadmin--npm");
	console.log("📟 配套后端安装方法: composer create-project sunjinglong/sunadmin");
	if (HAS_LOCAL_SUNADMIN_CORE) {
		console.log("\x1b[33m%s\x1b[0m", "\n📦 别名替换配置:");
		alias.forEach((item, index) => console.log(`👉 ${index}. ${item.find} → ${item.replacement}\x1b[0m`));
		console.log("\x1b[33m%s\x1b[0m", "\n");
	}
}
showWelcome();
